package com.yanqu.road.server.cmd.cross.marriage.world;

import com.yanqu.road.entity.marriage.CrossMarriageResult;
import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.logic.pb.MarriagePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.marriage.CrossMarriageProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.S_CROSS_WORLD_MARRIAGE_AGREE_REQ, desc = "跨服联姻同意请求（跨服商会分组）")
public class CrossWorldMarriageAgreeReqCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        CrossMarriageProto.CrossMarriageAgreeCrossDealReqMsg reqMsg = CrossMarriageProto.CrossMarriageAgreeCrossDealReqMsg.parseFrom(yanQuMessage.getBytes());
        MarriageInfo marriageInfo = MarriagePb.parseMarriageInfo(reqMsg.getApplyData());
        String partnerChildUid = reqMsg.getPartnerChildUid();
        long partnerUserId = yanQuMessage.getPlayerId();

        CrossMarriageResult marriageResult = MarriageMgr.toCrossWorldPartnerDealAgree(marriageInfo, partnerUserId, partnerChildUid);

        CrossMarriageProto.CrossMarriageAgreeCrossDealRespMsg.Builder respMsg = CrossMarriageProto.CrossMarriageAgreeCrossDealRespMsg.newBuilder();
        respMsg.setRet(marriageResult.getRet());
        respMsg.setApplyData(MarriagePb.parseMarriageApplyTempMsg(marriageInfo));
        if(0 == marriageResult.getRet()){
            respMsg.setPartnerData(MarriagePb.parseMarriageApplyTempMsg(marriageResult.getMarriageInfo()));
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_WORLD_MARRIAGE_AGREE_RESP, respMsg);
        GamePlayerMgr.sendPacket(0, pbMsg);

    }
}
